﻿Imports ATS.Business.Entities
Imports ATS.Data
Imports System.Collections.Generic

Public Class RFIDScanHistoryComponent


#Region "CRUD"

    Public Sub CreateRFIDScanHistory(_RFIDScanHistory As RFIDScanHistory, Optional _CreateAssetData As Boolean = False, Optional _CreatedBy As String = Nothing)
        Try
            CreateNewScanHistory(_RFIDScanHistory, _CreateASsetData, _CreatedBy)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Sub

    Public Function ReadRFIDScanHistoryByRFIDNumber(_RFIDNumber As String, Optional _RFIDScanDate As DateTime = Nothing) As List(Of RFIDScanHistory)
        Dim dac As New AssetDataAccess()
        Try
            Return dac.ReadRFIDScanHistoryByRFIDNumber(_RFIDNumber, _RFIDScanDate)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function ReadRFIDScanHistoryByScanDate(_scanDateFrom As Date, _scanDateTo As Date) As List(Of RFIDScanHistory)
        Dim dac As New AssetDataAccess()
        Try
            Return dac.ReadRFIDScanHistoryByScanDate(_scanDateFrom, _scanDateTo)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

#End Region

#Region "Support Procedures"
    Sub CreateNewScanHistory(_RFIDScanHistory As RFIDScanHistory, Optional _CreateAssetData As Boolean = False, Optional _CreatedBy As String = Nothing)
        Dim dac As New AssetDataAccess()
        Try
            dac.CreateRFIDScanHistory(_RFIDScanHistory)
            Dim selectedAsset As MainAssetsTable = dac.SelectAsset("", _RFIDScanHistory.RFIDNum)
            If _CreateAssetData And selectedAsset Is Nothing Then
                Dim assetData As New MainAssetsTable
                assetData.LastScanDate = _RFIDScanHistory.DateScanned
                assetData.Assetno = _RFIDScanHistory.RFIDNum
                assetData.DateCreated = Now
                assetData.Createdby = _CreatedBy
                assetData.LastModified = Now
                assetData.LastModifiedBy = _CreatedBy
                assetData.Assetno = _RFIDScanHistory.RFIDNum
                dac.Create(assetData)
            Else
                selectedAsset.LastScanDate = _RFIDScanHistory.DateScanned
                dac.Update(selectedAsset)
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Sub
#End Region
End Class
